get_town_adm_ms<-function(data,year){
  data_full<-data
  
  data<- as.data.frame(data_full %>% group_by(judet_ms,scoala_de_provenienta,scoala_de_provenienta_orig,an_adm,town_ms_adm) %>% ungroup())
  
  
  ##################################First part is for students who attend schools that are also high schools
  #Get town directly, when preceded by "city", "town", "village", etc
  data[grepl(".+,", data$scoala_de_provenienta),]$town_ms_adm<-sub(".+,", "", data$scoala_de_provenienta)[grepl(".+,", data$scoala_de_provenienta)]
  replace_string<-paste0(".+( ORASUL )|.+( COM )|.+( ORAS )|.+( MUNICIPIUL )|.+( LOC )|.+( LOCALITATEA )|",
                         ".+( \\(ENERGETIC\\) )|.+( \\(AUTO\\) )|.+( \\(EC\\. TEODOROIU\\) )|.+( ECONOMIC\\, ADMINISTRATIV SI DE SERVICII )|",
                         ".+( LOC. )|.+( MUN. )|.+( SAT )|.+( COMUNA )|.+( ORAȘ )|.+( MUN )|.+( COM\\. )|.+\" |.+\\d |.+\", |.+\"")
  data[grepl(replace_string, data$scoala_de_provenienta),]$town_ms_adm<-sub(replace_string, "", data[grepl(replace_string, data$scoala_de_provenienta),]$scoala_de_provenienta)
  #Get town for remaining obervations
  replace_string<-paste0(".+( TEHNOLOGIC DE TURISM SI ALIMENTATIE )|.+( MARMATIA )|.+( AL BANATULUI MONTAN )|",
                         ".+( PARTICULAR )|.+( DE ARTA )|.+( DEFICIENTI DE VEDERE )|.+( BILINGV ROMINO-CROAT )|",
                         ".+( DE CONSTRUCTII SI PROTECTIA MEDIULUI )|.+( PROFESIONALA DE COOPERATIE )|",
                         ".+( DE PROTECTIA MEDIULUI  )|.+( WALDORF )|(FEG )|.+( DE VEST )|",
                         ".+( PENTRU EDUCATIE INCLUZIVA )|.+( DE ARTE SI MESERII )|",
                         ".+( CONSTR. CAI FERATE )|.+( DE CHIMIE INDUSTRIALA )|",
                         ".+( CONSTRUCTII MONTAJ )|.+( INDUSTRIAL MINIER )|.+( MINIER )|",
                         ".+( DE COOPERATIE )|.+( INDUSTRIE USOARA )|.+( ECONOMIC-ADMINISTRATIV)|",
                         ".+( MESERII SI SERVICII )|.+( INDUSTRIE MICA SI SERVICII )|.+( NATIONALA DE GAZ )|",
                         ".+( DE MUZICA SI ARTE PLASTICE )|.+( CU PROGRAM DE ATLETISM )|.+( DE MUZICA )|",
                         ".+( ADMINISTRATIV SI DE SERVICII )|.+( ALIMENTATIE PUBLICA )|.+( CU PROGRAM SPORTIV )|",
                         ".+( FORESTIER )|.+( INDUSTRIA STICLEI )|.+( COOPERATIST )|.+( COOPERATIE )|",
                         ".+( DE MARINA )|.+( DE AGRICULTURA SI ECONOMIE )|.+( CONSTRUCTII CAI FERATE )|",
                         ".+( DE TRANSPORTURI AUTO )|.+( BILINGV ROMANO-CROAT )|.+( TEHNIC DE TRANSPORTURI )|",
                         ".+( SPECIAL PENTRU DEFICIENTI DE AUZ )|.+( ADVENTIST )|",
                         ".+( TRANSPORTURI CAI FERATE )|.+( VOCATIONAL DE ARTA )|.+( ROMANO-CATOLIC )|.+( SPECIAL )|.+( AUTO )|",
                         ".+( SILVIC )|.+( TEHNIC )|.+( CONSTRUCTII DE MASINI )|.+( METALURGIC )|.+( PETROL )|",
                         ".+( BANATEAN )|.+( CARASAN )|.+( ALIMENTARA )|.+( PENTICOSTAL )|.+( BAPTIST )|.+( AGRICOL )|.+( DE AFACERI )|",
                         ".+( REFORMAT )|.+( TEOLOGIC )|.+( ORTODOX )|.+( TEHNOLOGIC FORESTIER )|.+( TEHNOLOGIC CONSTRUCTII DE MASINI )|",
                         ".+( SPORTIV )|.+( SANITAR )|.+( ENERGETIC )|.+( TEHNOLOGIC )|.+( PEDAGOGIC )|.+( TEORETIC )|",
                         ".+( DE INFORMATICA )|.+( ECONOMIC )|.+( TRANSPORTURI )|.+( ADMINISTRATIV )|.+( DE TURISM )|.+( PENTRU TRANSPORT RUTIER )|",
                         ".+( MAGHIARA )|.+( TELECOMUNICATII )|.+( DE ARTE )|.+( CONSTRUCTII )|.+( SERVICII )|.+( AUTOMECANICA )|.+( NATIONAL )|",
                         ".+( FEROVIAR )|.+( TEXTIL )|(GR. SC. )|(CENTRUL SCOLAR )|(GRUP SCOLAR INDUSTRIAL )|",
                         ".+(THE CAMBRIDGE INTERNATIONAL SCHOOL IN )|.+( DIN )|(GRI )|(GRUP SCOLAR )|(GRUPUL SCOLAR INDUSTRIAL )|(GRUPUL SCOLAR )|(LICEUL )") 
  data[grepl(replace_string, data$scoala_de_provenienta) & is.na(data$town_ms_adm),]$town_ms_adm<-
    sub(replace_string, "", data[grepl(replace_string,data$scoala_de_provenienta) & is.na(data$town_ms_adm),]$scoala_de_provenienta)
  
  
  
  
  
  
  

    data$town_ms_adm<-if_else(data$judet_ms=='GIURGIU' & grepl("MIRON NICOLESCU",data$scoala_de_provenienta),"GIURGIU",data$town_ms_adm)

 
  data$town_ms_adm<-if_else(data$judet_ms=='BUZAU' & grepl("ALEXANDRU MARGHILOMAN",data$scoala_de_provenienta),"BUZAU",data$town_ms_adm)
  data$town_ms_adm<-if_else(data$judet_ms=='BACAU' & grepl("BC",data$scoala_de_provenienta),"BACAU",data$town_ms_adm)
  data$town_ms_adm<-if_else(data$judet_ms=='BIHOR' & grepl(" STEI",data$scoala_de_provenienta),"STEI",data$town_ms_adm)
  data$town_ms_adm<-if_else(data$judet_ms=='COVASNA' & grepl("INT\\. BUZAULUI",data$scoala_de_provenienta),"INTORSURA BUZAULUI",data$town_ms_adm)
  data$town_ms_adm<-if_else(data$judet_ms=='DOLJ' & grepl("MOFLENI",data$scoala_de_provenienta),"CRAIOVA",data$town_ms_adm)
  data$town_ms_adm<-if_else(data$judet_ms=='CLUJ' & grepl("GHERLA",data$scoala_de_provenienta),"GHERLA",data$town_ms_adm)
  data$town_ms_adm<-if_else(data$judet_ms=='CONSTANTA' & grepl("M. KOGALNICEANU",data$town_ms_adm),"MIHAIL KOGALNICEANU",data$town_ms_adm)
  data$town_ms_adm<-if_else(data$judet_ms=='CONSTANTA' & grepl("CALLATIS",data$town_ms_adm),"MANGALIA",data$town_ms_adm)
  data$town_ms_adm<-if_else(data$judet_ms=='GALATI' & grepl("PAUL DIMO",data$scoala_de_provenienta),"GALATI",data$town_ms_adm)
  data$town_ms_adm<-if_else(data$judet_ms=='IASI' & grepl("NICOLINA",data$scoala_de_provenienta),"IASI",data$town_ms_adm)
  data$town_ms_adm<-if_else(data$judet_ms=='IASI' & grepl("DEFICIENTI DE VEDERE",data$scoala_de_provenienta),"TARGU FRUMOS",data$town_ms_adm)
  data$town_ms_adm<-if_else(data$judet_ms=='MEHEDINTI' & grepl("DE MARINA",data$town_ms_adm),"ORSOVA",data$town_ms_adm)
  data$town_ms_adm<-if_else(data$judet_ms=='MEHEDINTI' & grepl("AUTO",data$town_ms_adm),"DROBETA-TURNU SEVERIN",data$town_ms_adm)
  data$town_ms_adm<-if_else(data$judet_ms=='SIBIU' & grepl("AVRIG",data$scoala_de_provenienta),"AVRIG",data$town_ms_adm)
  data$town_ms_adm<-if_else(data$judet_ms=='SIBIU' & grepl("BETANIA",data$scoala_de_provenienta),"SIBIU",data$town_ms_adm)
  data$town_ms_adm<-if_else(data$judet_ms=='VALCEA' & grepl("RM",data$scoala_de_provenienta),"RAMNICU VALCEA",data$town_ms_adm)
  data$town_ms_adm<-if_else(data$judet_ms=='VALCEA' & grepl("BABENI",data$scoala_de_provenienta),"BABENI",data$town_ms_adm)
  data$town_ms_adm<-if_else(data$judet_ms=='VALCEA' & grepl("BAILE OLANESTI",data$scoala_de_provenienta),"BAILE OLANESTI",data$town_ms_adm)
  


    data$town_ms_adm<-if_else(data$judet_ms=='MEHEDINTI' & grepl("AUTO",data$town_ms_adm),"DROBETA-TURNU SEVERIN",data$town_ms_adm)
    data$town_ms_adm<-if_else(data$judet_ms=='MEHEDINTI' & grepl("DE MARINA",data$town_ms_adm),"ORSOVA",data$town_ms_adm)
    data$town_ms_adm<-if_else(data$judet_ms=='DOLJ' & (grepl("ION MINCU",data$town_ms_adm)),"CRAIOVA",data$town_ms_adm)

  

    data$town_ms_adm<-if_else(data$judet_ms=='IALOMITA' & grepl("SCINTEIA",data$town_ms_adm),"SCANTEIA",data$town_ms_adm)
    data$town_ms_adm<-gsub('SANGERU','SINGERU',data$town_ms_adm)
    data$town_ms_adm<-if_else(data$judet_ms=='MARAMURES' & grepl("SPORTIV",data$town_ms_adm),"BAIA MARE",data$town_ms_adm)
    

  

    data$town_ms_adm<-if_else(data$judet_ms=='HUNEDOARA' & grepl("LUPENI",data$town_ms_adm),"LUPENI",data$town_ms_adm)
    data$town_ms_adm<-if_else(grepl('TRAIAN',data$scoala_de_provenienta) & grepl('MEHEDINTI',data$judet_ms),"DROBETA-TURNU SEVERIN",data$town_ms_adm)
    
    
  
  
  

    data$town_ms_adm<-if_else(data$judet_ms=='IASI' & grepl("WALDORF",data$town_ms_adm),"IASI",data$town_ms_adm)
    data$town_ms_adm<-if_else(data$judet_ms=='IASI' & grepl("COLEGIUL NATIONAL,",data$town_ms_adm),"IASI",data$town_ms_adm)
    data$town_ms_adm<-if_else(data$judet_ms=='NEAMT' & grepl("VINATORI",data$scoala_de_provenienta),"VANATORI NEAMT",data$town_ms_adm)
    
    data$town_ms_adm<-if_else(data$judet_ms=='MEHEDINTI' & grepl("TRAIAN",data$scoala_de_provenienta),"DROBETA-TURNU SEVERIN",data$town_ms_adm)
    
    data$town_ms_adm<-if_else(data$judet_ms=='MEHEDINTI' & grepl("COLEGIUL TEHNIC DE TRANSPORTURI AUTO",data$scoala_de_provenienta),"DROBETA-TURNU SEVERIN",data$town_ms_adm)
    

  

    data$town_ms_adm<-if_else(grepl('CURTEA DE ARGES',data$town_ms_adm),"CURTEA DE ARGES",data$town_ms_adm)
    
    data$town_ms_adm<-if_else(grepl('SEBES',data$town_ms_adm) & grepl('ALBA',data$judet_ms),"SEBES",data$town_ms_adm)
    
    data$town_ms_adm<-if_else(grepl('GEORGE VALSAN',data$scoala_de_provenienta) & data$judet_ms=='BRAILA',"BRAILA",data$town_ms_adm)
    data$town_ms_adm<-if_else(grepl('ANA ASLAN',data$scoala_de_provenienta) & data$judet_ms=='BRAILA',"BRAILA",data$town_ms_adm)
    data$town_ms_adm<-if_else(grepl('ION GHICA',data$scoala_de_provenienta) & data$judet_ms=='BRAILA',"BRAILA",data$town_ms_adm)
    data$town_ms_adm<-if_else(grepl('GHEORGHE MUNTEANU MURGOCI',data$scoala_de_provenienta) & data$judet_ms=='BRAILA',"BRAILA",data$town_ms_adm)
    data$town_ms_adm<-if_else(grepl('NICOLAE BALCESCU',data$scoala_de_provenienta) & data$judet_ms=='BRAILA',"BRAILA",data$town_ms_adm)
    data$town_ms_adm<-if_else(grepl('COSTIN D\\. NENITESCU',data$scoala_de_provenienta) & data$judet_ms=='BRAILA',"BRAILA",data$town_ms_adm)
    data$town_ms_adm<-if_else(grepl('EDMOND NICOLAU',data$scoala_de_provenienta) & data$judet_ms=='BRAILA',"BRAILA",data$town_ms_adm)
    data$town_ms_adm<-if_else(grepl('PANAIT ISTRATI',data$scoala_de_provenienta) & data$judet_ms=='BRAILA',"BRAILA",data$town_ms_adm)
    data$town_ms_adm<-if_else(grepl('HARICLEA DARCLEE',data$scoala_de_provenienta) & data$judet_ms=='BRAILA',"BRAILA",data$town_ms_adm)
    data$town_ms_adm<-if_else(grepl('D\\. P\\. PERPESSICIUS',data$scoala_de_provenienta) & data$judet_ms=='BRAILA',"BRAILA",data$town_ms_adm)
    data$town_ms_adm<-if_else(grepl('SPORTIV',data$scoala_de_provenienta) & data$judet_ms=='BRAILA',"BRAILA",data$town_ms_adm)
    data$town_ms_adm<-if_else(grepl('MIHAIL SEBASTIAN',data$scoala_de_provenienta) & data$judet_ms=='BRAILA',"BRAILA",data$town_ms_adm)
    data$town_ms_adm<-if_else(grepl('NICOLAE IORGA',data$scoala_de_provenienta) & data$judet_ms=='BRAILA',"BRAILA",data$town_ms_adm)
    data$town_ms_adm<-if_else(grepl('PANAIT CERNA',data$scoala_de_provenienta) & data$judet_ms=='BRAILA',"BRAILA",data$town_ms_adm)
    data$town_ms_adm<-if_else(grepl('ANGHEL SALIGNY',data$scoala_de_provenienta) & data$judet_ms=='BRAILA',"BRAILA",data$town_ms_adm)
    data$town_ms_adm<-if_else(grepl('GHEORGHE K\\. CONSTANTINESCU',data$scoala_de_provenienta) & data$judet_ms=='BRAILA',"BRAILA",data$town_ms_adm)
    data$town_ms_adm<-if_else(grepl('GRIGORE MOISIL',data$scoala_de_provenienta) & data$judet_ms=='BRAILA',"BRAILA",data$town_ms_adm)
    
    data$town_ms_adm<-if_else(grepl('NICOLAE ONCESCU',data$scoala_de_provenienta) & data$judet_ms=='BRAILA',"IANCA",data$town_ms_adm)
    data$town_ms_adm<-if_else(grepl('CONSTANTIN ANGELESCU',data$scoala_de_provenienta) & data$judet_ms=='BRAILA',"IANCA",data$town_ms_adm)
    
    data$town_ms_adm<-if_else(grepl('GEORGE VILSAN',data$scoala_de_provenienta) & data$judet_ms=='BRAILA',"FAUREI",data$town_ms_adm)
    
    data$town_ms_adm<-if_else(grepl('SCOALA PROFESIONALA SIG',data$scoala_de_provenienta) & data$judet_ms=='SALAJ',"SAG",data$town_ms_adm)
    
    data$town_ms_adm<-if_else(data$scoala_de_provenienta=="LICEUL TEHNOLOGIC" & data$judet_ms=='MEHEDINTI',"DROBETA-TURNU SEVERIN",data$town_ms_adm)
    

  
  
  
  
  #ADJUSTMENTS BY COUNTY
  names<-unique(data$judet_ms)
  data<-data %>% 
    group_split(judet_ms)
  names(data)<-names
  
  
  
  
  
  
  data[['ALBA']]$town_ms_adm<-if_else(grepl("SEBES",data[['ALBA']]$scoala_de_provenienta),"SEBES",data[['ALBA']]$town_ms_adm)
  
  data[['ARAD']]$town_ms_adm<-if_else(grepl("ADAM MULLER GUTTENBRUNN",data[['ARAD']]$scoala_de_provenienta),"ARAD",data[['ARAD']]$town_ms_adm)
  data[['ARAD']]$town_ms_adm<-if_else(grepl("CONCORD",data[['ARAD']]$scoala_de_provenienta),"ARAD",data[['ARAD']]$town_ms_adm)
  
  data[['ARGES']]$town_ms_adm<-if_else(grepl("ALEXANDRU IONESCU",data[['ARGES']]$scoala_de_provenienta),"PITESTI",data[['ARGES']]$town_ms_adm) #2016
  data[['ARGES']]$town_ms_adm<-if_else(data[['ARGES']]$scoala_de_provenienta=="LICEUL TEHNOLOGIC","VEDEA",data[['ARGES']]$town_ms_adm)
  data[['ARGES']]$town_ms_adm<-if_else(grepl("PETRESCU",data[['ARGES']]$scoala_de_provenienta),"STALPENI",data[['ARGES']]$town_ms_adm)
  data[['ARGES']]$town_ms_adm<-if_else(grepl("CURTEA",data[['ARGES']]$scoala_de_provenienta),"CURTEA DE ARGES",data[['ARGES']]$town_ms_adm)
  data[['ARGES']]$town_ms_adm<-if_else(grepl("PETRE IONESCU MUSCEL",data[['ARGES']]$scoala_de_provenienta),"DOMNESTI",data[['ARGES']]$town_ms_adm)
  data[['ARGES']]$town_ms_adm<-if_else(grepl("DINU BRATIANU",data[['ARGES']]$scoala_de_provenienta),"STEFANESTI",data[['ARGES']]$town_ms_adm)
  data[['ARGES']]$town_ms_adm<-if_else(grepl("TEIULEANU",data[['ARGES']]$scoala_de_provenienta),"PITESTI",data[['ARGES']]$town_ms_adm)
  data[['ARGES']]$town_ms_adm<-if_else(grepl("BRANCUSI",data[['ARGES']]$scoala_de_provenienta),"PITESTI",data[['ARGES']]$town_ms_adm)
  data[['ARGES']]$town_ms_adm<-if_else(grepl("DOBRESCU",data[['ARGES']]$scoala_de_provenienta),"CURTEA DE ARGES",data[['ARGES']]$town_ms_adm)
  data[['ARGES']]$town_ms_adm<-if_else(grepl("ELIM",data[['ARGES']]$scoala_de_provenienta),"PITESTI",data[['ARGES']]$town_ms_adm)
  data[['ARGES']]$town_ms_adm<-if_else(grepl("DACIA",data[['ARGES']]$scoala_de_provenienta),"PITESTI",data[['ARGES']]$town_ms_adm)
  data[['ARGES']]$town_ms_adm<-if_else(grepl("ASTRA",data[['ARGES']]$scoala_de_provenienta),"PITESTI",data[['ARGES']]$town_ms_adm)
  data[['ARGES']]$town_ms_adm<-if_else(grepl("DINU LIPATTI",data[['ARGES']]$scoala_de_provenienta),"PITESTI",data[['ARGES']]$town_ms_adm)
  data[['ARGES']]$town_ms_adm<-if_else(grepl("VIITORUL",data[['ARGES']]$scoala_de_provenienta),"PITESTI",data[['ARGES']]$town_ms_adm)
  data[['ARGES']]$town_ms_adm<-if_else((grepl("IUSTIN PATRIARHUL",data[['ARGES']]$scoala_de_provenienta)),"CAMPULUNG",data[['ARGES']]$town_ms_adm)
  data[['ARGES']]$town_ms_adm<-if_else((grepl("NEAGOE VODA BASARAB",data[['ARGES']]$scoala_de_provenienta)),"CURTEA DE ARGES",data[['ARGES']]$town_ms_adm)
  data[['ARGES']]$town_ms_adm<-if_else(grepl("MIHALACHE",data[['ARGES']]$scoala_de_provenienta),"TOPOLOVENI",data[['ARGES']]$town_ms_adm)
  data[['ARGES']]$town_ms_adm<-if_else(grepl("CANTACUZINO",data[['ARGES']]$scoala_de_provenienta),"PITESTI",data[['ARGES']]$town_ms_adm)
  data[['ARGES']]$town_ms_adm<-if_else(grepl("BARBU",data[['ARGES']]$scoala_de_provenienta),"PITESTI",data[['ARGES']]$town_ms_adm)
  data[['ARGES']]$town_ms_adm<-if_else(grepl("CAROL",data[['ARGES']]$scoala_de_provenienta),"CAMPULUNG",data[['ARGES']]$town_ms_adm)
  data[['ARGES']]$town_ms_adm<-if_else(grepl("BARBILIAN",data[['ARGES']]$scoala_de_provenienta),"CAMPULUNG",data[['ARGES']]$town_ms_adm)
  data[['ARGES']]$town_ms_adm<-if_else(grepl("NENITESCU",data[['ARGES']]$scoala_de_provenienta),"PITESTI",data[['ARGES']]$town_ms_adm)
  data[['ARGES']]$town_ms_adm<-if_else(grepl("ARMAND",data[['ARGES']]$scoala_de_provenienta),"PITESTI",data[['ARGES']]$town_ms_adm)
  data[['ARGES']]$town_ms_adm<-if_else(grepl("VLAICU VODA",data[['ARGES']]$scoala_de_provenienta),"CURTEA DE ARGES",data[['ARGES']]$town_ms_adm)
  data[['ARGES']]$town_ms_adm<-if_else(grepl("ION C. BRATIANU",data[['ARGES']]$scoala_de_provenienta),"PITESTI",data[['ARGES']]$town_ms_adm)
  data[['ARGES']]$town_ms_adm<-if_else(grepl("REBREANU",data[['ARGES']]$scoala_de_provenienta),"MOZACENI",data[['ARGES']]$town_ms_adm)
  data[['ARGES']]$town_ms_adm<-if_else(grepl("SLAVESCU",data[['ARGES']]$scoala_de_provenienta),"RUCAR",data[['ARGES']]$town_ms_adm)
  data[['ARGES']]$town_ms_adm<-if_else(grepl("VITEAZUL",data[['ARGES']]$scoala_de_provenienta),"PITESTI",data[['ARGES']]$town_ms_adm)
  data[['ARGES']]$town_ms_adm<-if_else(grepl("VLAICU VODA",data[['ARGES']]$scoala_de_provenienta),"CURTEA DE ARGES",data[['ARGES']]$town_ms_adm)
  data[['ARGES']]$town_ms_adm<-if_else(grepl("COLIBASI",data[['ARGES']]$scoala_de_provenienta),"MIOVENI",data[['ARGES']]$town_ms_adm)
  
  
  data[['BACAU']]$town_ms_adm<-if_else(grepl("ASACHI",data[['BACAU']]$scoala_de_provenienta),"ONESTI",data[['BACAU']]$town_ms_adm)
  data[['BACAU']]$town_ms_adm<-if_else(grepl("STEFAN CEL MARE",data[['BACAU']]$scoala_de_provenienta),"BACAU",data[['BACAU']]$town_ms_adm)
  
  data[['BOTOSANI']]$town_ms_adm<-if_else((grepl("GRUPUL SCOLAR DE COOPERATIE",data[['BOTOSANI']]$scoala_de_provenienta)),"BOTOSANI",data[['BOTOSANI']]$town_ms_adm)
  data[['BOTOSANI']]$town_ms_adm<-if_else((grepl("CENTRUL DE STUDII STEFAN CEL MARE SI SFANT",data[['BOTOSANI']]$scoala_de_provenienta)),"BOTOSANI",data[['BOTOSANI']]$town_ms_adm)
  data[['BOTOSANI']]$town_ms_adm<-if_else(grepl("SPORTIV",data[['BOTOSANI']]$scoala_de_provenienta),"BOTOSANI",data[['BOTOSANI']]$town_ms_adm)
  data[['BOTOSANI']]$town_ms_adm<-if_else(grepl("INDUSTRIE USOARA$",data[['BOTOSANI']]$scoala_de_provenienta),"BOTOSANI",data[['BOTOSANI']]$town_ms_adm)
  data[['BOTOSANI']]$town_ms_adm<-if_else(grepl("ORTODOX",data[['BOTOSANI']]$scoala_de_provenienta) & grepl("SFANTUL GHEORGHE",data[['BOTOSANI']]$scoala_de_provenienta),"BOTOSANI",data[['BOTOSANI']]$town_ms_adm)
  data[['BOTOSANI']]$town_ms_adm<-if_else(grepl("STEFAN LUCHIAN",data[['BOTOSANI']]$scoala_de_provenienta),"BOTOSANI",data[['BOTOSANI']]$town_ms_adm)
  data[['BOTOSANI']]$town_ms_adm<-if_else(grepl("ORTODOX",data[['BOTOSANI']]$scoala_de_provenienta)& (grepl("IOAN IACOB",data[['BOTOSANI']]$scoala_de_provenienta)),"DOROHOI",data[['BOTOSANI']]$town_ms_adm)
  data[['BOTOSANI']]$town_ms_adm<-if_else(grepl("PETRU RARES",data[['BOTOSANI']]$scoala_de_provenienta),"BOTOSANI",data[['BOTOSANI']]$town_ms_adm)
  data[['BOTOSANI']]$town_ms_adm<-if_else(grepl("NEGREANU",data[['BOTOSANI']]$scoala_de_provenienta),"BOTOSANI",data[['BOTOSANI']]$town_ms_adm)
  data[['BOTOSANI']]$town_ms_adm<-if_else(grepl("CIUCA",data[['BOTOSANI']]$scoala_de_provenienta),"SAVENI",data[['BOTOSANI']]$town_ms_adm)
  data[['BOTOSANI']]$town_ms_adm<-if_else(grepl("IORGA",data[['BOTOSANI']]$scoala_de_provenienta),"BOTOSANI",data[['BOTOSANI']]$town_ms_adm)
  
  data[['BIHOR']]$town_ms_adm<-if_else(grepl("BOCOCI",data[['BIHOR']]$scoala_de_provenienta),"ORADEA",data[['BIHOR']]$town_ms_adm)
  data[['BIHOR']]$town_ms_adm<-if_else(grepl("SACUIENI",data[['BIHOR']]$scoala_de_provenienta),"SACUENI",data[['BIHOR']]$town_ms_adm)
  data[['BIHOR']]$town_ms_adm<-if_else(grepl("STEI",data[['BIHOR']]$scoala_de_provenienta),"STEI",data[['BIHOR']]$town_ms_adm)
  data[['BIHOR']]$town_ms_adm<-if_else(grepl("CIORDAS",data[['BIHOR']]$scoala_de_provenienta),"BEIUS",data[['BIHOR']]$town_ms_adm)
  
  data[['BISTRITA-NASAUD']]$town_ms_adm<-if_else(grepl("SANITAR",data[['BISTRITA-NASAUD']]$scoala_de_provenienta),"BISTRITA",data[['BISTRITA-NASAUD']]$town_ms_adm)
  data[['BISTRITA-NASAUD']]$town_ms_adm<-if_else(grepl("BISTRTA",data[['BISTRITA-NASAUD']]$scoala_de_provenienta),"BISTRITA",data[['BISTRITA-NASAUD']]$town_ms_adm)
  
  data[['BRAILA']]$town_ms_adm<-if_else(grepl("MATEI BASARAB",data[['BRAILA']]$scoala_de_provenienta),"MAXINENI",data[['BRAILA']]$town_ms_adm)
  
  data[['BRASOV']]$town_ms_adm<-if_else((grepl("DOAMNA STANCA",data[['BRASOV']]$scoala_de_provenienta)),"FAGARAS",data[['BRASOV']]$town_ms_adm)
  data[['BRASOV']]$town_ms_adm<-if_else((grepl("FELDIOARA",data[['BRASOV']]$scoala_de_provenienta)),"FELDIOARA",data[['BRASOV']]$town_ms_adm)
  
  data[['BUZAU']]$town_ms_adm<-if_else(grepl("RADU VLADESCU",data[['BUZAU']]$scoala_de_provenienta),"PATARLAGELE",data[['BUZAU']]$town_ms_adm)
  data[['BUZAU']]$town_ms_adm<-if_else(grepl("DEFICIENTI DE VEDERE",data[['BUZAU']]$scoala_de_provenienta),"BUZAU",data[['BUZAU']]$town_ms_adm)
  data[['BUZAU']]$town_ms_adm<-if_else(grepl("IOLANDA",data[['BUZAU']]$scoala_de_provenienta),"BUZAU",data[['BUZAU']]$town_ms_adm)
  
  data[['CLUJ']]$town_ms_adm<-if_else((grepl("OCTAVIAN STROIA",data[['CLUJ']]$scoala_de_provenienta)),"CLUJ-NAPOCA",data[['CLUJ']]$town_ms_adm)
  data[['CLUJ']]$town_ms_adm<-if_else(grepl("CLUJ-NAPOCA",data[['CLUJ']]$scoala_de_provenienta),"CLUJ-NAPOCA",data[['CLUJ']]$town_ms_adm)
  
  data[['CONSTANTA']]$town_ms_adm<-if_else(grepl("GEORGE CALINESCU",data[['CONSTANTA']]$scoala_de_provenienta),"CONSTANTA",data[['CONSTANTA']]$town_ms_adm)
  data[['CONSTANTA']]$town_ms_adm<-if_else(grepl("EFORIE",data[['CONSTANTA']]$scoala_de_provenienta),"EFORIE",data[['CONSTANTA']]$town_ms_adm)
  data[['CONSTANTA']]$town_ms_adm<-if_else(grepl("HURMUZESCU",data[['CONSTANTA']]$scoala_de_provenienta),"MEDGIDIA",data[['CONSTANTA']]$town_ms_adm)
  
  data[['COVASNA']]$town_ms_adm<-if_else((grepl("BERDE ARON",data[['COVASNA']]$scoala_de_provenienta)),"SFANTU GHEORGHE",data[['COVASNA']]$town_ms_adm)
  
  data[['DOLJ']]$town_ms_adm<-if_else((grepl("ANGHEL SALIGNY",data[['DOLJ']]$scoala_de_provenienta)),"CRAIOVA",data[['DOLJ']]$town_ms_adm)
  data[['DOLJ']]$town_ms_adm<-if_else((grepl("ION MINCU",data[['DOLJ']]$scoala_de_provenienta)),"CRAIOVA",data[['DOLJ']]$town_ms_adm)
  
  data[['GALATI']]$town_ms_adm<-if_else((grepl("DIMITRIE CANTEMIR",data[['GALATI']]$scoala_de_provenienta)),"GALATI",data[['GALATI']]$town_ms_adm)
  
  data[['GORJ']]$town_ms_adm<-if_else((grepl("BILTA",data[['GORJ']]$town_ms_adm)|grepl("RUNCU",data[['GORJ']]$town_ms_adm)),"BILTA, RUNCU",data[['GORJ']]$town_ms_adm)
  data[['GORJ']]$town_ms_adm<-if_else((grepl("ION MINCU",data[['GORJ']]$scoala_de_provenienta)),"TARGU-JIU",data[['GORJ']]$town_ms_adm)
  
  data[['GIURGIU']]$town_ms_adm<-if_else(grepl("TEOCTIST",data[['GIURGIU']]$scoala_de_provenienta),"GIURGIU",data[['GIURGIU']]$town_ms_adm)
  data[['GIURGIU']]$town_ms_adm<-if_else(grepl("MIRON NICOLESCU",data[['GIURGIU']]$scoala_de_provenienta),"GIURGIU",data[['GIURGIU']]$town_ms_adm)
  data[['GIURGIU']]$town_ms_adm<-if_else(grepl("IOAN BALANESCU",data[['GIURGIU']]$scoala_de_provenienta),"GIURGIU",data[['GIURGIU']]$town_ms_adm)
  data[['GIURGIU']]$town_ms_adm<-if_else(grepl("ION BARBU",data[['GIURGIU']]$scoala_de_provenienta),"GIURGIU",data[['GIURGIU']]$town_ms_adm)
  
  
  data[['HUNEDOARA']]$town_ms_adm<-if_else(grepl("HUNEDOARA",data[['HUNEDOARA']]$scoala_de_provenienta),"HUNEDOARA",data[['HUNEDOARA']]$town_ms_adm)
  data[['HUNEDOARA']]$town_ms_adm<-if_else(grepl("LUPENI",data[['HUNEDOARA']]$scoala_de_provenienta),"LUPENI",data[['HUNEDOARA']]$town_ms_adm)
  
  data[['ILFOV']]$town_ms_adm<-if_else(grepl("MARK TWAIN",data[['ILFOV']]$scoala_de_provenienta),"VOLUNTARI",data[['ILFOV']]$town_ms_adm)
  data[['ILFOV']]$town_ms_adm<-if_else(grepl("HULUBEI",data[['ILFOV']]$scoala_de_provenienta),"MAGURELE",data[['ILFOV']]$town_ms_adm)
  data[['ILFOV']]$town_ms_adm<-if_else(grepl("DOAMNA CHIAJNA",data[['ILFOV']]$scoala_de_provenienta),"ROSU",data[['ILFOV']]$town_ms_adm)
  data[['ILFOV']]$town_ms_adm<-if_else(grepl("DUMITRU DUMITRESCU",data[['ILFOV']]$scoala_de_provenienta),"MAGURELE",data[['ILFOV']]$town_ms_adm)
  data[['ILFOV']]$town_ms_adm<-if_else(grepl("VINTILA BRATIANU",data[['ILFOV']]$scoala_de_provenienta),"DRAGOMIRESTI-VALE",data[['ILFOV']]$town_ms_adm)
  data[['ILFOV']]$town_ms_adm<-if_else(grepl("ROSETTI",data[['ILFOV']]$scoala_de_provenienta),"VIDRA",data[['ILFOV']]$town_ms_adm)
  data[['ILFOV']]$town_ms_adm<-if_else(grepl("LALESCU",data[['ILFOV']]$scoala_de_provenienta),"BRANESTI",data[['ILFOV']]$town_ms_adm)
  data[['ILFOV']]$town_ms_adm<-if_else(grepl("IOAN PETRUS",data[['ILFOV']]$scoala_de_provenienta),"OTOPENI",data[['ILFOV']]$town_ms_adm)
  data[['ILFOV']]$town_ms_adm<-if_else(grepl("PAMFIL SEICARU",data[['ILFOV']]$scoala_de_provenienta),"CIOROGARLA",data[['ILFOV']]$town_ms_adm)
  data[['ILFOV']]$town_ms_adm<-if_else(grepl("MIHAIL KOGALNICEANU",data[['ILFOV']]$scoala_de_provenienta),"SNAGOV",data[['ILFOV']]$town_ms_adm)
  data[['ILFOV']]$town_ms_adm<-if_else(grepl("STIRBEY",data[['ILFOV']]$scoala_de_provenienta),"BUFTEA",data[['ILFOV']]$town_ms_adm)
  data[['ILFOV']]$town_ms_adm<-if_else(grepl("NICOLAE BALCESCU",data[['ILFOV']]$scoala_de_provenienta),"VOLUNTARI",data[['ILFOV']]$town_ms_adm)
  data[['ILFOV']]$town_ms_adm<-if_else(grepl("CEZAR NICOLAU",data[['ILFOV']]$scoala_de_provenienta),"BRANESTI",data[['ILFOV']]$town_ms_adm)
  data[['ILFOV']]$town_ms_adm<-if_else(grepl("FILOFTEIA",data[['ILFOV']]$scoala_de_provenienta),"BRANESTI",data[['ILFOV']]$town_ms_adm)
  data[['ILFOV']]$town_ms_adm<-if_else(grepl("PIETRARU",data[['ILFOV']]$scoala_de_provenienta),"BRANESTI",data[['ILFOV']]$town_ms_adm)
  
  data[['IASI']]$town_ms_adm<-if_else((grepl("SCHEIA",data[['IASI']]$town_ms_adm)),"SCHEIA",data[['IASI']]$town_ms_adm)
  data[['IASI']]$town_ms_adm<-if_else((grepl("PASCANI",data[['IASI']]$town_ms_adm)),"PASCANI",data[['IASI']]$town_ms_adm)
  data[['IASI']]$town_ms_adm<-if_else((grepl("TARGU FRUMOS",data[['IASI']]$town_ms_adm)),"TARGU FRUMOS",data[['IASI']]$town_ms_adm)
  data[['IASI']]$town_ms_adm<-if_else((grepl("NICOLINA",data[['IASI']]$town_ms_adm)),"IASI",data[['IASI']]$town_ms_adm)
  
  data[['IALOMITA']]$town_ms_adm<-if_else((grepl("FIERBINTI",data[['IALOMITA']]$town_ms_adm)),"FIERBINTI",data[['IALOMITA']]$town_ms_adm)
  data[['IALOMITA']]$town_ms_adm<-if_else(grepl("ORTODOX",data[['IALOMITA']]$scoala_de_provenienta) & grepl("IOAN GURA DE AUR",data[['IALOMITA']]$scoala_de_provenienta),"SLOBOZIA",data[['IALOMITA']]$town_ms_adm)
  
  data[['MEHEDINTI']]$town_ms_adm<-if_else(grepl("TRANSPORTURI AUTO",data[['MEHEDINTI']]$scoala_de_provenienta),"DROBETA-TURNU SEVERIN",data[['MEHEDINTI']]$town_ms_adm)
  data[['MEHEDINTI']]$town_ms_adm<-if_else(grepl("GHEORGHE TITEICA",data[['MEHEDINTI']]$scoala_de_provenienta),"DROBETA-TURNU SEVERIN",data[['MEHEDINTI']]$town_ms_adm)
  data[['MEHEDINTI']]$town_ms_adm<-if_else(grepl("COLEGIUL NATIONAL \"TRAIAN\"",data[['MEHEDINTI']]$scoala_de_provenienta),"DROBETA-TURNU SEVERIN",data[['MEHEDINTI']]$town_ms_adm)
  data[['MEHEDINTI']]$town_ms_adm<-if_else(grepl("THEODOR COSTESCU",data[['MEHEDINTI']]$scoala_de_provenienta),"DROBETA-TURNU SEVERIN",data[['MEHEDINTI']]$town_ms_adm)
  data[['MEHEDINTI']]$town_ms_adm<-if_else(grepl("STEFAN ODOBLEJA",data[['MEHEDINTI']]$scoala_de_provenienta),"DROBETA-TURNU SEVERIN",data[['MEHEDINTI']]$town_ms_adm)
  data[['MEHEDINTI']]$town_ms_adm<-if_else(grepl("DECEBAL",data[['MEHEDINTI']]$scoala_de_provenienta),"DROBETA-TURNU SEVERIN",data[['MEHEDINTI']]$town_ms_adm)
  data[['MEHEDINTI']]$town_ms_adm<-if_else(grepl("TUDOR",data[['MEHEDINTI']]$scoala_de_provenienta),"DROBETA-TURNU SEVERIN",data[['MEHEDINTI']]$town_ms_adm)
  data[['MEHEDINTI']]$town_ms_adm<-if_else(grepl("SERBAN CIOCULESCU",data[['MEHEDINTI']]$scoala_de_provenienta),"DROBETA-TURNU SEVERIN",data[['MEHEDINTI']]$town_ms_adm)
  data[['MEHEDINTI']]$town_ms_adm<-if_else(grepl("DIERNA",data[['MEHEDINTI']]$scoala_de_provenienta),"ORSOVA",data[['MEHEDINTI']]$town_ms_adm)
  data[['MEHEDINTI']]$town_ms_adm<-if_else(grepl("PAULIAN",data[['MEHEDINTI']]$scoala_de_provenienta),"DROBETA-TURNU SEVERIN",data[['MEHEDINTI']]$town_ms_adm)
  data[['MEHEDINTI']]$town_ms_adm<-if_else(grepl("IONESCU",data[['MEHEDINTI']]$scoala_de_provenienta),"SISESTI",data[['MEHEDINTI']]$town_ms_adm)
  data[['MEHEDINTI']]$town_ms_adm<-if_else(grepl("VLADIMIRESCU",data[['MEHEDINTI']]$scoala_de_provenienta),"TUDOR VLADIMIRESCU",data[['MEHEDINTI']]$town_ms_adm)
  data[['MEHEDINTI']]$town_ms_adm<-if_else(grepl("PAULIAN",data[['MEHEDINTI']]$scoala_de_provenienta),"DROBETA-TURNU SEVERIN",data[['MEHEDINTI']]$town_ms_adm)
  data[['MEHEDINTI']]$town_ms_adm<-if_else(grepl("CONSTANTIN",data[['MEHEDINTI']]$scoala_de_provenienta),"BAIA DE ARAMA",data[['MEHEDINTI']]$town_ms_adm)
  data[['MEHEDINTI']]$town_ms_adm<-if_else(grepl("BASARAB",data[['MEHEDINTI']]$scoala_de_provenienta),"STREHAIA",data[['MEHEDINTI']]$town_ms_adm)
  data[['MEHEDINTI']]$town_ms_adm<-if_else(grepl("GOMOIU",data[['MEHEDINTI']]$scoala_de_provenienta),"VANJU MARE",data[['MEHEDINTI']]$town_ms_adm)
  data[['MEHEDINTI']]$town_ms_adm<-if_else(grepl("LALESCU",data[['MEHEDINTI']]$scoala_de_provenienta),"ORSOVA",data[['MEHEDINTI']]$town_ms_adm)
  data[['MEHEDINTI']]$town_ms_adm<-if_else(grepl("SALAGEAN",data[['MEHEDINTI']]$scoala_de_provenienta),"DROBETA-TURNU SEVERIN",data[['MEHEDINTI']]$town_ms_adm)
  data[['MEHEDINTI']]$town_ms_adm<-if_else(grepl("INDUSTRIAL DE MARINA",data[['MEHEDINTI']]$scoala_de_provenienta),"ORSOVA",data[['MEHEDINTI']]$town_ms_adm)
  data[['MEHEDINTI']]$town_ms_adm<-if_else(grepl("AUTO",data[['MEHEDINTI']]$scoala_de_provenienta),"DROBETA-TURNU SEVERIN",data[['MEHEDINTI']]$town_ms_adm)
  
  
  data[['MURES']]$town_ms_adm<-if_else(grepl("ROMANO-AMERICANA",data[['MURES']]$scoala_de_provenienta),"TARGU-MURES",data[['MURES']]$town_ms_adm)#2014
  data[['MURES']]$town_ms_adm<-if_else(grepl("JEBELEAN",data[['MURES']]$scoala_de_provenienta),"SANNICOLAU-MARE",data[['MURES']]$town_ms_adm)#2014
  data[['MURES']]$town_ms_adm<-if_else(grepl("TARGU-MURES",data[['MURES']]$scoala_de_provenienta),"TARGU MURES",data[['MURES']]$town_ms_adm)#2014
  
  
  data[['NEAMT']]$town_ms_adm<-if_else(grepl("VASILE CONTA",data[['NEAMT']]$scoala_de_provenienta),"TARGU NEAMT",data[['NEAMT']]$town_ms_adm)
  data[['NEAMT']]$town_ms_adm<-if_else(grepl("MANASTIREA NEAMT",data[['NEAMT']]$scoala_de_provenienta),"VANATORI NEAMT",data[['NEAMT']]$town_ms_adm)
  data[['NEAMT']]$town_ms_adm<-if_else(grepl("BOLINTIN",data[['NEAMT']]$scoala_de_provenienta),"BOLINTIN-VALE",data[['NEAMT']]$town_ms_adm)
  data[['NEAMT']]$town_ms_adm<-if_else(grepl("ION CREANGA",data[['NEAMT']]$scoala_de_provenienta) & grepl('COLEGIUL TEHNIC',data[['NEAMT']]$scoala_de_provenienta),"TARGU NEAMT",data[['NEAMT']]$town_ms_adm)
  
  data[['PRAHOVA']]$town_ms_adm<-if_else(grepl("BOLDESTI",data[['PRAHOVA']]$scoala_de_provenienta),"BOLDESTI-SCAENI",data[['PRAHOVA']]$town_ms_adm)
  data[['PRAHOVA']]$town_ms_adm<-if_else(grepl("MANECIU",data[['PRAHOVA']]$scoala_de_provenienta),"MANECIU",data[['PRAHOVA']]$town_ms_adm)
  data[['PRAHOVA']]$town_ms_adm<-if_else(grepl("CIORANI",data[['PRAHOVA']]$scoala_de_provenienta),"CIORANI",data[['PRAHOVA']]$town_ms_adm)
  
  
  data[['SALAJ']]$town_ms_adm<-if_else(grepl("CSEREY-GOGA",data[['SALAJ']]$scoala_de_provenienta),"CRASNA",data[['SALAJ']]$town_ms_adm)
  data[['SALAJ']]$town_ms_adm<-if_else(grepl("SIG",data[['SALAJ']]$town_ms_adm),"SAG",data[['SALAJ']]$town_ms_adm)
  
  data[['SATU-MARE']]$town_ms_adm<-if_else((grepl("SATU MARE",data[['SATU-MARE']]$scoala_de_provenienta)),"SATU MARE",data[['SATU-MARE']]$town_ms_adm)
  
  data[['SIBIU']]$town_ms_adm<-if_else((grepl("CIBINIUM",data[['SIBIU']]$scoala_de_provenienta)),"SIBIU",data[['SIBIU']]$town_ms_adm)
  data[['SIBIU']]$town_ms_adm<-if_else((grepl("COPSA MICA",data[['SIBIU']]$scoala_de_provenienta)),"COPSCA MICA",data[['SIBIU']]$town_ms_adm)
  data[['SIBIU']]$town_ms_adm<-if_else((grepl("AVRIG",data[['SIBIU']]$scoala_de_provenienta)),"AVRIG",data[['SIBIU']]$town_ms_adm)
  
  data[['SUCEAVA']]$town_ms_adm<-if_else((grepl("CENTRUL DE STUDII STEFAN CEL MARE SI SFANT",data[['SUCEAVA']]$town_ms_adm)),"SUCEAVA",data[['SUCEAVA']]$town_ms_adm)
  data[['SUCEAVA']]$town_ms_adm<-if_else(grepl("MOLDOVENESC",data[['SUCEAVA']]$scoala_de_provenienta),"CAMPULUNG MOLDOVENESC",data[['SUCEAVA']]$town_ms_adm)
  data[['SUCEAVA']]$town_ms_adm<-if_else(grepl("PIRTESTII",data[['SUCEAVA']]$scoala_de_provenienta),"PARTESTII DE JOS",data[['SUCEAVA']]$town_ms_adm)
  
  data[['TELEORMAN']]$town_ms_adm<-if_else(grepl("ALEXANRIA",data[['TELEORMAN']]$scoala_de_provenienta),"ALEXANDRIA",data[['TELEORMAN']]$town_ms_adm)
  data[['TELEORMAN']]$town_ms_adm<-if_else(grepl("DRAGANESTI",data[['TELEORMAN']]$scoala_de_provenienta),"DRAGANESTI-VLASCA",data[['TELEORMAN']]$town_ms_adm)
  data[['TELEORMAN']]$town_ms_adm<-if_else(grepl("ROSIORI",data[['TELEORMAN']]$scoala_de_provenienta),"ROSIORII DE VEDE",data[['TELEORMAN']]$town_ms_adm)
  
  data[['VASLUI']]$town_ms_adm<-if_else(grepl("BIRLAD",data[['VASLUI']]$scoala_de_provenienta),"BARLAD",data[['VASLUI']]$town_ms_adm)
  
  data[['TIMIS']]$town_ms_adm<-sub(' TIMIS$',"",data[['TIMIS']]$town_ms_adm)
  data[['TIMIS']]$town_ms_adm<-if_else(grepl("TIMISOARA",data[['TIMIS']]$scoala_de_provenienta),"TIMISOARA",data[['TIMIS']]$town_ms_adm)
  data[['TIMIS']]$town_ms_adm<-if_else(grepl("JEBELEAN",data[['TIMIS']]$scoala_de_provenienta),"SANNICOLAU MARE",data[['TIMIS']]$town_ms_adm)
  data[['TIMIS']]$town_ms_adm<-if_else(grepl("SHAKESPEARE",data[['TIMIS']]$scoala_de_provenienta),"TIMISOARA",data[['TIMIS']]$town_ms_adm)
  data[['TIMIS']]$town_ms_adm<-if_else(grepl("BILED",data[['TIMIS']]$scoala_de_provenienta),"BILED",data[['TIMIS']]$town_ms_adm)
  
  data[['VALCEA']]$town_ms_adm<-if_else( data[['VALCEA']]$town_ms_adm=="","RAMNICU VALCEA",data[['VALCEA']]$town_ms_adm)
  data[['VALCEA']]$town_ms_adm<-if_else( is.na(data[['VALCEA']]$town_ms_adm),"RAMNICU VALCEA",data[['VALCEA']]$town_ms_adm)
  data[['VALCEA']]$town_ms_adm<-if_else( grepl("BUZESCU",data[['VALCEA']]$scoala_de_provenienta),"BERBESTI",data[['VALCEA']]$town_ms_adm)
  data[['VALCEA']]$town_ms_adm<-if_else( grepl("IUSTINIAN",data[['VALCEA']]$scoala_de_provenienta),"BAILE OLANESTI",data[['VALCEA']]$town_ms_adm)
  data[['VALCEA']]$town_ms_adm<-if_else( grepl("OLANESTI",data[['VALCEA']]$scoala_de_provenienta),"BAILE OLANESTI",data[['VALCEA']]$town_ms_adm)
  data[['VALCEA']]$town_ms_adm<-if_else( grepl("SF NICOLAE",data[['VALCEA']]$scoala_de_provenienta),"RAMNICU VALCEA",data[['VALCEA']]$town_ms_adm)
  data[['VALCEA']]$town_ms_adm<-if_else( grepl("SFANTU NICOLAE",data[['VALCEA']]$scoala_de_provenienta),"RAMNICU VALCEA",data[['VALCEA']]$town_ms_adm)
  data[['VALCEA']]$town_ms_adm<-if_else( grepl("MATEI BASARAB",data[['VALCEA']]$scoala_de_provenienta),"RAMNICU VALCEA",data[['VALCEA']]$town_ms_adm)
  data[['VALCEA']]$town_ms_adm<-if_else( grepl("GHEORGHE SURDU",data[['VALCEA']]$scoala_de_provenienta),"BREZOI",data[['VALCEA']]$town_ms_adm)
  data[['VALCEA']]$town_ms_adm<-if_else( grepl("BRATIANU",data[['VALCEA']]$scoala_de_provenienta),"DRAGASANI",data[['VALCEA']]$town_ms_adm)
  data[['VALCEA']]$town_ms_adm<-if_else( grepl("TARNEA",data[['VALCEA']]$scoala_de_provenienta),"BABENI",data[['VALCEA']]$town_ms_adm)
  data[['VALCEA']]$town_ms_adm<-if_else( grepl("BABENI",data[['VALCEA']]$scoala_de_provenienta),"BABENI",data[['VALCEA']]$town_ms_adm)
  data[['VALCEA']]$town_ms_adm<-if_else( grepl("OLTCHIM",data[['VALCEA']]$scoala_de_provenienta),"RAMNICU VALCEA",data[['VALCEA']]$town_ms_adm)
  data[['VALCEA']]$town_ms_adm<-if_else( grepl("FERDINAND",data[['VALCEA']]$scoala_de_provenienta),"RAMNICU VALCEA",data[['VALCEA']]$town_ms_adm)
  data[['VALCEA']]$town_ms_adm<-if_else( grepl("RM",data[['VALCEA']]$scoala_de_provenienta),"RAMNICU VALCEA",data[['VALCEA']]$town_ms_adm)
  data[['VALCEA']]$town_ms_adm<-if_else(grepl("VICTOR GIUREANU",data[['VALCEA']]$scoala_de_provenienta),"RAMNICU VALCEA",data[['VALCEA']]$town_ms_adm)
  
  
  data[['VRANCEA']]$town_ms_adm<-if_else(data[['VRANCEA']]$judet_ms=='VRANCEA' & grepl("ASACHI",data[['VRANCEA']]$scoala_de_provenienta),"FOCSANI",data[['VRANCEA']]$town_ms_adm)
  
  data<-do.call("rbind",data)
  
  #ADJUSTMENTS BY TOWN NAME
  data$town_ms_adm<-if_else(grepl("CLUJ-NAPOCA",data$scoala_de_provenienta),"CLUJ-NAPOCA",data$town_ms_adm)
  data$town_ms_adm<-if_else(grepl("RAMNICU VALCEA",data$scoala_de_provenienta),"RAMNICU VALCEA",data$town_ms_adm)
  data$town_ms_adm<-if_else(grepl("PITESTI",data$scoala_de_provenienta),"PITESTI",data$town_ms_adm)
  data$town_ms_adm<-if_else((grepl("ORADEA",data$town_ms_adm)),"ORADEA",data$town_ms_adm)
  data$town_ms_adm<-if_else((grepl("BIRLAD",data$town_ms_adm)),"BIRLAD",data$town_ms_adm)
  data$town_ms_adm<-if_else((grepl("T. VLADIMIRESCU",data$town_ms_adm)),"TUDOR VLADIMIRESCU",data$town_ms_adm)
  data$town_ms_adm<-if_else(grepl("OCNA-MURES",data$town_ms_adm),"OCNA MURES",data$town_ms_adm)
  data$town_ms_adm<-if_else(grepl("SINNICOLAU",data$town_ms_adm)|grepl("SANNICOLAU",data$town_ms_adm),"SANNICOLAU-MARE",data$town_ms_adm)
  data$town_ms_adm<-if_else(grepl("SINMARTIN",data$town_ms_adm),"SANMARTIN",data$town_ms_adm)
  data$town_ms_adm<-if_else(grepl("HALINGA",data$town_ms_adm),"HALANGA",data$town_ms_adm)
  data$town_ms_adm<-if_else(grepl("PIATRA NEAMT",data$town_ms_adm),'PIATRA-NEAMT',data$town_ms_adm)
  
  
  data$town_ms_adm<-if_else(grepl("BAIAMARE",data$town_ms_adm),"BAIA MARE",data$town_ms_adm)
  
  data$town_ms_adm<-if_else(data$town_ms_adm=='TG MURES','TARGU MURES',data$town_ms_adm)
  data$town_ms_adm<-if_else(data$town_ms_adm=='NEGRESTI OAS','NEGRESTI-OAS',data$town_ms_adm)
  data$town_ms_adm<-if_else(data$town_ms_adm=='TG-JIU','TARGU-JIU',data$town_ms_adm)
  data$town_ms_adm<-if_else(data$town_ms_adm=='TG JIU','TARGU-JIU',data$town_ms_adm)
  data$town_ms_adm<-if_else(data$judet_ms=='BUCUREȘTI','BUCURESTI',data$town_ms_adm)
  data$town_ms_adm<-if_else(data$judet_ms=='BUCURESTI','BUCURESTI',data$town_ms_adm)
  data$town_ms_adm<-gsub('CIMPULUNG','CAMPULUNG',data$town_ms_adm)
  data$town_ms_adm<-gsub('CHISINEU CRIS','CHISINEU-CRIS',data$town_ms_adm)
  data$town_ms_adm<-gsub('TG\\. FRUMOS','TARGU FRUMOS',data$town_ms_adm)
  data$town_ms_adm<-gsub('RIMNICU','RAMNICU',data$town_ms_adm)
  data$town_ms_adm<-gsub('RM\\.','RAMNICU',data$town_ms_adm)
  data$town_ms_adm<-gsub("TIRGU","TARGU",data$town_ms_adm)
  data$town_ms_adm<-gsub("SFINTU","SFANTU",data$town_ms_adm)
  data$town_ms_adm<-gsub("TIRG","TARG",data$town_ms_adm)
  data$town_ms_adm<-gsub("SFANTUL GHEORGHE","SFANTU GHEORGHE",data$town_ms_adm)
  data$town_ms_adm<-if_else(data$town_ms_adm=='CLUJ','CLUJ-NAPOCA',data$town_ms_adm)
  
  data$town_ms_adm<-if_else(data$town_ms_adm=='CURTEA DE AG','CURTEA DE ARGES',data$town_ms_adm)
  
  
  #SUB TG. AND SF
  data$town_ms_adm<-sub("TG\\.","TARGU",data$town_ms_adm)
  data$town_ms_adm<-sub("SF\\.","SFANTU",data$town_ms_adm)
  
  data$town_ms_adm<-sub("PARTICULAR","",data$town_ms_adm)
  
  #################Second part is for hthe other schools
  school_words<-c('SCOALA CU CLASELE I-VIII |SCOALA GIMNAZIALA |SCOALA GENERALA CLASELE I-VIII| SCOALA CLASELE I-VIII |SCOALA GENERALA |SCOALA CU CL\\. I-VIII\\. |SCOALA CU CL\\. I-VIII ')
  data[grepl(school_words, data$scoala_de_provenienta) & is.na(data$town_ms_adm),]$town_ms_adm<-
    sub(school_words, "", data[grepl(school_words,data$scoala_de_provenienta) & is.na(data$town_ms_adm),]$scoala_de_provenienta)

  
  #FINAL ADJUSTMENTS
  data$town_ms_adm<-trimws(data$town_ms_adm)
  
  #find towns in the data and replace those towns that might have residual school names in them, by county
  towns<-data %>% group_by(judet_ms,town_ms_adm) %>% summarize(n=n()) %>% filter(n>2 & !is.na(town_ms_adm) & town_ms_adm!='')
  for (county in unique(towns$judet_ms)){
    towns_temp<- towns %>% filter(judet_ms==county)
    for (town in towns_temp$town_ms_adm){
      data<-data %>% rowwise() %>% mutate(town_ms_adm=ifelse(judet_ms==county & grepl(town,town_ms_adm),town,town_ms_adm))
    }
  }
 
  
  
  
  data_full<-data_full %>% select(-town_ms_adm)
  data_full<-base::merge(data_full,data,by=c("judet_ms","an_adm","scoala_de_provenienta","scoala_de_provenienta_orig"))
  
  data_full<-data_full %>% 
    mutate(town_ms_adm=if_else(scoala_de_provenienta=='NO HS MATCHED' & scoala_de_provenienta=='','NOT ADMITTED',town_ms_adm)) %>%
    mutate(town_ms_adm=if_else(scoala_de_provenienta=='NO HS MATCHED' & scoala_de_provenienta!='','NO HS MATCHED',town_ms_adm)) 
  
  
  
  
  
  #TESTS:
  #1. COUNTIES, TOWNS AND NUMBER OF STUDENTS
  #test<-data_bac_raw %>% group_by(judet_ms,town_ms_adm) %>% summarize(n=n()) %>% arrange(judet_ms,n)
  #2. DEBUG: ALL DATA WITH TOWNS THAT CONTAIN SPECIFIC PATTERN
  #test2<-data_bac_raw[grepl("MESERII",data_bac_raw$town_ms_adm),]
  #2.1 DEBUG: ALL DATA WITH TOWNS THAT CONTAIN SPECIFIC PATTERN IN CERTAIN COUNTY
  #test2<-data_bac_raw[grepl("COOPERATIE",data_bac_raw$scoala_de_provenienta) & data_bac_raw$judet_ms=="BOTOSANI",]
  #3. DATA WITHOUT TOWN
  #test2<-data_bac_raw[is.na(data_bac_raw$town_ms_adm) & !is.na(data_bac_raw$nume),] %>% group_by(scoala_de_provenienta) %>%
  #  summarize(n=n())
  
  return(data_full)
}